import os

from flask import Flask, render_template
from app.blueprints import testbp, user
from app.extensions import db

def create_app():
    app = Flask(__name__,
                instance_path=os.path.join(os.path.abspath(os.curdir),'instance'),
                instance_relative_config=True)
    app.config.from_pyfile('../instance/config.cfg')
    db.init_app(app)

    @app.route('/')
    def index():
        app.logger.info('index page')
        return render_template('index.html')

    @app.route('/calculate/<string:expression>')
    def calculate(expression):
        app.logger.info(f'expression: {expression}')
        return str(eval(expression))

    app.register_blueprint(testbp.bp, url_prefix='/test')
    app.logger.info('loaded test blueprint')

    app.register_blueprint(user.bp, url_prefix='/user')

    return app